package com.ease.core.components {
   import com.ease.core.events.DockingEvent;

   import flash.display.DisplayObject;
   import flash.display.DisplayObjectContainer;

   import mx.containers.BoxDirection;
   import mx.containers.DividedBox;
   import mx.core.ScrollPolicy;
   import mx.core.UIComponent;
   import mx.events.DividerEvent;

   /** A window with a split pane that contains two child dock windows */
   public class SplitWindow extends DividedBox implements DockingWindow {

      private var _originalWidth:Number = -1;
      public function get originalWidth():Number {
         return _originalWidth;
      }
      public function set originalWidth(value:Number):void {
         _originalWidth = value;
      }

      private var _originalHeight:Number = -1;
      public function get originalHeight():Number {
         return _originalHeight;
      }
      public function set originalHeight(value:Number):void {
         _originalHeight = value;
      }

      private var _originalParent:DisplayObjectContainer = null;
      public function get originalParent():DisplayObjectContainer {
         return _originalParent;
      }
      public function set originalParent(value:DisplayObjectContainer):void {
         _originalParent = value;
      }

      public function saveOriginalContext():void {
         originalParent = parent;
         originalWidth = isNaN(percentWidth) ? width * 100 / parent.width : percentWidth;
         originalHeight = isNaN(percentHeight) ? height * 100 / parent.height : percentHeight;
      }

      public function restoreOriginalContext():void {
         if (originalParent && !originalParent.contains(this)) {
            originalParent.addChild(this);
         }
         percentWidth = originalWidth;
         percentHeight = originalHeight;
      }

      public function SplitWindow() {
         super();

         addEventListener(DockingEvent.DOCKED, onViewDocked);
         addEventListener(DockingEvent.REMOVED, onViewRemoved);
         addEventListener(DividerEvent.DIVIDER_RELEASE, onChildResize);

         setStyle("verticalGap", 3);
         setStyle("horizontalGap", 3);
         setStyle("verticalAlign", "middle");

         setStyle("horizontalAlign", "center");
         setStyle("dividerAlpha", 0);

         percentWidth = 100;
         percentHeight = 100;
         liveDragging = true;
         resizeToContent = true;

         horizontalScrollPolicy = ScrollPolicy.OFF;
         verticalScrollPolicy = ScrollPolicy.OFF;
      }

      private function onChildResize(e:*):void {

      }

      public function onViewRemoved(event:DockingEvent):void {
         var splitWindowParent:DisplayObjectContainer = parent as DisplayObjectContainer;
         if (!(splitWindowParent is StageWindow)) {
            if (numChildren < 2) {
               if (numChildren == 1) {
                  //	var view:
                  var displayObject:UIComponent = getChildAt(0) as UIComponent;
                  displayObject.percentWidth = 100;
                  displayObject.percentHeight = 100;
                  splitWindowParent.addChildAt(displayObject, splitWindowParent.getChildIndex(this));
               }
               splitWindowParent.removeChild(this);
            }
         }
      }

      public function setDirection(directionValue:String):void {
         if (BoxDirection.HORIZONTAL == directionValue) {
            setHorizontal();
         }
         else {
            setVertical();
         }
      }

      public function setHorizontal():void {
         direction = BoxDirection.HORIZONTAL;
      }

      public function setVertical():void {
         direction = BoxDirection.VERTICAL;
      }

      public function isHorizontal():Boolean {
         return BoxDirection.HORIZONTAL == direction;
      }

      public function isVertical():Boolean {
         return BoxDirection.VERTICAL == direction;
      }

      private function onViewDocked(event:DockingEvent):void {
         if (numChildren == 1) {
            parent.addChild(getChildAt(0));
            parent.removeChild(this);
         }
      }

      private function onChildRemove(event:DockingEvent):void {
         if (numChildren == 0) {
            parent.removeChild(this); //TODO Editor
         }
         if (numChildren == 1) {
            parent.addChild(getChildAt(0));
            parent.removeChild(this);
         }
      }
   }
}